package main

import (
	"context"
	"errors"
	"fmt"
	"log"

	service "firstwork/server/gen/go/pet/v1"

	"google.golang.org/grpc"
)

func main() {
	conn, err := grpc.Dial(":8002", grpc.WithInsecure())
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	} //连接失败
	defer conn.Close()
	client := service.NewPetStoreService(conn)
	var input int
	for true {
		fmt.Println("请输入数字转到相应功能")
		fmt.Println("1:GetPet")
		fmt.Println("2:PutPet")
		fmt.Println("3:DeletePet")
		fmt.Println("4:exit")
		_, err = fmt.Scan(&input)
		if err != nil {
			fmt.Println(err)
		}
		var id string
		switch input {
		case 1:
			{

				fmt.Println("请输入需要查询的宠物ID编号")
				_, _ = fmt.Scan(&id)
				resp, err := client.GetPet(context.Background(), &service.GetPetRequest{
					PetId: id,
				})
				if err != nil {
					log.Println(err)
				}
				pet := resp.Pet
				dt := pet.CreatedAt
				fmt.Println("宠物名" + pet.Name)
				fmt.Print("宠物类型:")
				switch pet.PetType {
				case 0:
					fmt.Println("其他种类宠物")
				case 1:
					fmt.Println("宠物猫")
				case 2:
					fmt.Println("宠物狗")
				case 3:
					fmt.Println("宠物蛇")
				case 4:
					fmt.Println("宠物仓鼠")
				}
				fmt.Println("宠物ID" + pet.PetId)
				fmt.Println("收录时间:%d年%d月%d日%d时%d分%d秒\n", dt.Year, dt.Month, dt.Day, dt.Hours, dt.Minutes, dt.Seconds)
			}
		case 2:
			{
				fmt.Println("请输入宠物类型：" + "0:其他种类宠物" + "1:宠物猫" + "2:宠物狗" + "3:宠物蛇" + "4:宠物仓鼠")
				_, err = fmt.Scanln(&input)
				if err != nil {
					log.Println(err)
				}
				if input < 0 || input > 4 {
					log.Println(errors.New("宠物类型编号输入错误！"))
				}
				fmt.Println("请输入宠物名称！")
				var inputname string
				fmt.Scanln(&inputname)
				putPetResponse, err := client.PutPet(context.Background(), &service.PutPetRequest{
					PetType: service.PetType(input),
					Name:    inputname,
				})
				if err != nil {
					log.Println(err)
				}
				fmt.Println("已添加ID为 " + putPetResponse.Pet.PetId + " 的宠物")
			}
		case 3:
			fmt.Println()
			fmt.Println("请输入要删除的宠物ID编号")
			_, err = fmt.Scanln(&id)
			if err != nil {
				log.Println(err)
			}
			deletepetresponse, err := client.DeletePet(context.Background(), &service.DeletePetRequest{
				PetId: id,
			})
			if err != nil {
				log.Println(errors.New("删除失败！请检查是否有此宠物"))
			}
			fmt.Println("已删除ID为 " + id + " 的宠物")
			fmt.Println()

		case 4:
			return
		}
	}
}
